package com.mj.starsignpair.service;

import java.util.LinkedHashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.mj.starsignpair.util.HttpUtils;

/**
 * 星座配对测试
 * @author zhaominglei
 * @date 2015-4-9
 * 
 */
public class StarsignpairService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = StarsignpairService.class.getSimpleName();
	private static final String M_SHEUP_XINGZUO_PEIDUI_URL = "http://m.sheup.com/xingzuo_peidui_1.php"; //三藏算命网手机版
	@SuppressWarnings("serial")
	public Map<String, String> map = new LinkedHashMap<String, String>() {
		{
			put("白羊座", "1");
			put("金牛座", "2");
			put("双子座", "3");
			put("巨蟹座", "4");
			put("狮子座", "5");
			put("处女座", "6");
			put("天秤座", "7");
			put("天蝎座", "8");
			put("射手座", "9");
			put("摩羯座", "10");
			put("水瓶座", "11");
			put("双鱼座", "12");
		}
	};
	
	public String getStarsignpairInfo(String boyStar, String girlStar) {
		if (boyStar == null || boyStar.equals("") 
				|| girlStar == null || girlStar.equals("")) {
			return null;
		}
		String html = pair(boyStar, girlStar);
		if (html != null && !html.equals("")) {
			Document document = Jsoup.parse(html);
			Elements elements = document.getElementsByClass("sanzang_subs");
			if (elements != null && !elements.isEmpty()) {
				StringBuilder result = new StringBuilder();
				int i = 0;
				for (Element element : elements) {
					String content = element.html();
					
					if (i == 0) {
						result.append(content);
						break;
					}
					i++;
				}
				return result.toString();
			}
		}
		
		return null;
	}
	
	public String pair(String boyStar, String girlStar) {
		StringBuilder param = new StringBuilder();
		param.append("xingzuo=").append(map.get(boyStar));
		param.append("&astro=").append(map.get(girlStar));
		param.append("&peidui=%D0%C7%D7%F9%C5%E4%B6%D4%B2%E9%D1%AF");
		String html = HttpUtils.doPostForMSheup(M_SHEUP_XINGZUO_PEIDUI_URL, param.toString(), "gbk");
		return html;
	}
}
